﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using BusinessLayer;
using EntityModels;

namespace PresentationLayer
{
    public partial class EditProfile : AbstractPage
    {
        private User loggedInUser = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                loggedInUser = GetUserFromCookie();
            }
            catch (Exception ex)
            {
                this.debugMessage.InnerHtml += ex.Message;
            }
        }

        protected void Page_LoadComplete(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.EMail.Text = loggedInUser.email;
                this.Password.Attributes["Text"] = loggedInUser.password;
                this.ConfirmPassword.Attributes["Text"] = loggedInUser.password;
                this.FirstName.Text = loggedInUser.firstname;
                this.LastName.Text = loggedInUser.lastname;
                this.AboutMe.Text = loggedInUser.about;
                this.FullNameCB.Checked = (bool)loggedInUser.showname;
                this.EmailCB.Checked = (bool)loggedInUser.showemail;
                this.PrivateProfileCB.Checked = (bool)loggedInUser.privateprofile;
                this.MyEventsCB.Checked = (bool)loggedInUser.showmyevents;
                this.AttendingEventsCB.Checked = (bool)loggedInUser.showattevents;
            }
        }

        protected void UpdateDetails(object sender, EventArgs e)
        {
            try
            {
                if ((Password.Text != "") && (Password.Text == ConfirmPassword.Text))
                {
                    loggedInUser.firstname = FirstName.Text;
                    loggedInUser.lastname = LastName.Text;
                    loggedInUser.email = EMail.Text;
                    loggedInUser.password = Password.Text;
                    loggedInUser.showname = FullNameCB.Checked;
                    loggedInUser.showemail = EmailCB.Checked;
                    loggedInUser.privateprofile  =PrivateProfileCB.Checked;
                    loggedInUser.showmyevents = MyEventsCB.Checked;
                    loggedInUser.showattevents = AttendingEventsCB.Checked;

                    Controller.Instance.UpdateUser(loggedInUser);

                    Response.Redirect("Profile.aspx");
                }
                
            }
            catch (Exception ex)
            {
                this.debugMessage.InnerHtml += ex.Message;
            }
        }

        protected void CancelUpdate(object sender, EventArgs e)
        {
            Response.Redirect("Profile.aspx");
        }
    }
}